﻿// See https://aka.ms/new-console-template for more information
using SmartPlant.WinCore;
using SmartPlant.WinCore.Helper;
using System.Net;
using System.Net.Http;

var propertyInfos = new List<DynamicPropertyInfo>()
{
    new DynamicPropertyInfo{ PropertyName="Id",PropertyType="long" },
    new DynamicPropertyInfo{ PropertyName="StudentNo",PropertyType="int" },
    new DynamicPropertyInfo{ PropertyName="Level",PropertyType="int" },
    new DynamicPropertyInfo{ PropertyName="Workdate",PropertyType="DateTime" },
    new DynamicPropertyInfo{ PropertyName="UserName",PropertyType="string" },
    new DynamicPropertyInfo{ PropertyName="Sex",PropertyType="bool" },
    new DynamicPropertyInfo{ PropertyName="JobTime",PropertyType="DateTime" },
    new DynamicPropertyInfo{ PropertyName="PhonePower",PropertyType="string" },
};
string className = "Person";
var assembly = DynamicClassHelper.CreateAssembly(className, propertyInfos);
if (assembly == null) return;
dynamic objclass = assembly.CreateInstance(className);

Console.WriteLine("Hello, World!");

byte[] xlsFileContent = await FileHelper.ReadFileContentAsync("C:\\Users\\Administrator\\Desktop\\新建文件夹\\功能清单.xls");
byte[] xmlFileContent = await FileHelper.ReadFileContentAsync("C:\\Users\\Administrator\\Desktop\\新建文件夹\\国网宁夏电力有限公司配电专业信息化项目周报（第八期）20230224(1).docx");
string[] fileNames = new string[2] { "1111.xlsx", "2222.xml" };
List<byte[]> bytes = new List<byte[]>() { xlsFileContent, xmlFileContent };
string[] names = new string[2] { "key1", "key2" };

await PostFileAsync(bytes, names, fileNames);


/// <summary>
/// 向远程服务器上传文件。
/// </summary>
/// <param name="requestUri">远程服务器URL。</param>
/// <param name="bytes">文件内容对象。</param>
/// <param name="fileNames">上传的文件名数组。</param>
/// <returns></returns>
async Task<bool> PostFileAsync(List<byte[]> bytes, string[] names, string[] fileNames)
{
    if (bytes.Count != fileNames.Length)
        throw new ArgumentException($"bytes参数的长度必须与fileNames的长度一致。");

    string contentType = "multipart/form-data";
    var url = "http://localhost:5000/api/Project/designdocument/Uploader";

    string boundary = string.Format("--{0}", Guid.NewGuid());
    using var client = new HttpClient();

    OnInitRequestHeader(client, contentType);
    var content = new MultipartFormDataContent(boundary);
    for (int i = 0; i < bytes.Count; i++)
    {
        //MemoryStream memoryStream = new MemoryStream(bytes[i]);
        //StreamContent streamContent = new StreamContent(memoryStream);
        var byteContent = new ByteArrayContent(bytes[i], 0, bytes[i].Length);
        byteContent.Headers.Add("Content-Type", "application/octet-stream");
        //byteContent.Headers.Add("Content-Disposition", string.Format($"form-data; name={names[i]}; filename={fileNames[i]}"));
        content.Add(byteContent, names[i], fileNames[i]);
    }
    client.DefaultRequestHeaders.Add("Method", "Post");
    client.DefaultRequestHeaders.Add("KeepAlive", "false");// HTTP KeepAlive设为false，防止HTTP连接保持

    var response = await client.PostAsync(url, content);
    if (response.StatusCode != HttpStatusCode.OK)
    {
        throw new HttpRequestException();
    }
    response.EnsureSuccessStatusCode();
    var result = await response.Content.ReadAsStringAsync();
    return true;
}

void OnInitRequestHeader(HttpClient httpClient, string mediaType)
{
    //httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(mediaType));
    httpClient.DefaultRequestHeaders.Add("Accept", mediaType);
    httpClient.DefaultRequestHeaders.Add("ContentType", mediaType);
    httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows 10.0.22621.1265; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/110.0.835.163 Safari/535.1");

}